<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Page_Task extends Controller_Page_Template {

	public function action_index()
	{
		
	}
	public function action_overview($user_id)
	{
		$project_id = $_GET['project_id'];
		$functions = array(
			array('controller' => '/t/overview/'.$user_id.'?project_id='.$project_id, 'name' => 'Overview'),
			array('controller' => '/t/add_task/'.$user_id.'?project_id='.$project_id, 'name' => 'Add task'),
			array('controller' => '/t/remove_task/'.$user_id.'?project_id='.$project_id, 'name' => 'Remove task'),
		); 
		$this->add_sidebar($functions);
		$project = Model::factory('project')->get_project_info($project_id);
		$this->project = $project;
		$tasks = Model::factory('task')->get_user_tasks($user_id, $project_id);
		$this->primary_content = View::factory('/main/task/overview', array(
			'tasks' 	=> $tasks,
			'project'	=> $project,
			'user_id'	=> $user_id,
		));
	}
	public function action_add_task($user_id)
	{
		$project_id = $_GET['project_id'];
		$functions = array(
			array('controller' => '/t/overview/'.$user_id.'?project_id='.$project_id, 'name' => 'Overview'),
			array('controller' => '/t/add_task/'.$user_id.'?project_id='.$project_id, 'name' => 'Add task'),
			array('controller' => '/t/remove_task/'.$user_id.'?project_id='.$project_id, 'name' => 'Remove task'),
		); 
		$this->add_sidebar($functions);
		$project = Model::factory('project')->get_project_info($project_id);
		$this->project = $project;
		$tasks = Model::factory('task')->get_user_tasks($user_id, $project_id);
		$this->primary_content = View::factory('/main/task/add_task', array(
			'user_id'	=> $user_id,
			'tasks' 	=> $tasks,
			'project'	=> $project,
			'users'		=> Model::factory('user')->get_project_users($project_id, 1),
		));
	}
	public function action_submit_add_task($user_id)
	{
		$m = Model::factory('task');
		$project_id = $_GET['project_id'];
		$ret = $m->add_task(
			$_POST['user_id'], $_POST['name'], $_POST['description'], '', $_POST['deadline']);
		$m->assign_task($ret[0], $_POST['user_id'], $project_id, $_POST['deadline']);
		$this->request->redirect('/t/overview/'.$user_id.'?project_id='.$project_id);
	}
} 